<?php

namespace app\controllers\admin;

use app\commands\ErrorMsg;
use app\commands\Helper;
use app\commands\Validator;
use app\Services\admin\UserService;
use yii\web\Controller;
use app\commands\IdCard;


class UserController extends Controller
{
    public $req = array();
    public $user = array();

    /**
     * 安全 校验 屏蔽
     * 预处理  权限等等操作处理
     */
    public function beforeAction($action){
        $this->req = Helper::in(array_merge($_POST, $_GET));
        $this->user = Helper::checksign($this->req);
        unset($this->req['token']);
        return true;
    }

    /**
     * 用户列表 默认第一页 一页10条
     */
    public function actionGetList(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'Level'  => "required|integer|in:1,2",
            'num'    => 'integer',
            'page'   => 'integer',
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        $res = UserService::getInstance()->getUserList($req);
        Helper::right_out($req,$res);
    }

    /**
     * 用户详情
     */
    public function actionGetInfo(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'userID' => "required|integer",
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        $res = UserService::getInstance()->getUserInfo($req['userID']);
        Helper::right_out($req,$res);
    }

    /**
     * 保存用户信息
     */
    public function actionSaveInfo(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
//            'Nickname' => 'required',
            'Name'     => "required",
//            'Company'  => 'required',
//            'Position' => 'required',
//            'EnglishName'   =>'required|alpha_num',
//            'EnglishCompany'=>'required|alpha_num',
//            'EnglishPosition'=>'required|alpha_num',
            'Cellphone'=> "regex:/^1[345789][0-9]{9}$/",
            "email"    => "email",
            "CountryID"=> "integer",
            "userID"   => "integer",
            "IDcard"   => 'alpha_num'
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        if (!empty($req['IDcard'])){
            $idCard = new IdCard();
            if (!$idCard->isChinaIDCard($req['IDcard'])){
                Helper::right_out($req,['code'=>'-1','message'=>'身份证号有误,请重新输入']);
            }
        }
        $res = UserService::getInstance()->saveUserInfo($req);
        Helper::right_out($req,$res);
    }

    /**
     * 封禁/解封
     */
    public function actionActiveUser(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'userID'   => "required|integer",
            'Activity' => "required|in:0,1"
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        $res = UserService::getInstance()->activeUser($req);
        Helper::right_out($req,$res);
    }


    /**
     * 获取全部用户数据
     */
    public function actionGetAllList(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'num'    => 'integer',
            'page'   => 'integer',
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        $res = UserService::getInstance()->getAllList($req);
        Helper::right_out($req,$res);
    }

}
